package com.luckyframe.project.jmeter.execution.callbacks;

import com.alibaba.fastjson.JSON;
import com.luckyframe.common.utils.spring.SpringUtils;
import com.luckyframe.project.jmeter.jmeterTestPlan.context.CaseRunningContext;
import com.luckyframe.project.jmeter.jmeterTestPlan.context.TestPlanListener;
import com.luckyframe.project.jmeter.jmeterTestPlanRecord.domain.JmeterTestPlanRecord;
import com.luckyframe.project.jmeter.jmeterTestPlanRecord.enums.JmeterPlanRecordStatus;
import com.luckyframe.project.jmeter.jmeterTestPlanRecord.service.IJmeterTestPlanRecordService;
import org.apache.jmeter.report.core.DataContext;

public class CaseResultDataHandlerImpl implements CaseResultDataHandler {

    private IJmeterTestPlanRecordService planRecordService= SpringUtils.getBean(IJmeterTestPlanRecordService.class);

    private TestPlanListener testPlanListener;

    public void setTestPlanListener(TestPlanListener testPlanListener) {
        this.testPlanListener = testPlanListener;
    }


    @Override
    public void handle(DataContext dataContext, CaseRunningContext caseRunningContext) {

        JmeterTestPlanRecord planRecord = planRecordService.selectJmeterTestPlanRecordById(caseRunningContext.getRecordId());

        if(planRecord!=null){
            String resultData = JSON.toJSONString(dataContext);
            planRecord.setResultData(resultData);
            planRecord.setStatus(JmeterPlanRecordStatus.SUCCESS.getCode());
            planRecordService.updateJmeterTestPlanRecord(planRecord);
        }
    }
}
